package com.zl.tby.vo;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.model.TbyAnimalPlant;
import com.zl.tby.model.TbySpeciesCategory;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 物种分类Bean
 * @author changpeng
 * @version 1.0
 * @since 2024/8/8 23:07
 */
@Data
public class TbySpeciesCategoryBean extends TbySpeciesCategory {
    private List<TbySpeciesCategoryBean> children;

    public static List<TbySpeciesCategoryBean> recurData(Long parentId, List<TbySpeciesCategory> dbList) {
        List<TbySpeciesCategoryBean> resultList = new ArrayList<>();
        for (TbySpeciesCategory entity : dbList) {
            TbySpeciesCategoryBean vo = new TbySpeciesCategoryBean();
            vo.setId(entity.getId());
            vo.setCategoryName(entity.getCategoryName());
            vo.setParentId(entity.getParentId());
            vo.setUseOverview(entity.getUseOverview());
            if (vo.getParentId().compareTo(parentId) == 0) {
                List<TbySpeciesCategoryBean> children = recurData(vo.getId(), dbList);
                vo.setChildren(children);
                resultList.add(vo);
            }
        }
        return resultList;
    }

    public static List<OptionType> recurRouteOptions(Long parentId, List<TbySpeciesCategory> dataList) {
        List<OptionType> resultList = ToolsUtil.emptyIfNull(dataList).stream()
                .filter(bean -> bean.getParentId().equals(parentId))
                .map(bean -> new OptionType(bean.getId(), bean.getCategoryName(), recurRouteOptions(bean.getId(), dataList)))
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        return resultList;
    }
}
